﻿
package cn.zniot.so.database;

import cn.zniot.so.application.util.MethodUtil;

import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 抽象数据库访问器
 */
public abstract class AbstractDAO {
    private final Logger logger = Logger.getLogger(AbstractDAO.class);

    /**
     * 开启事务操作
     */
    protected void beginTransaction(Connection conn) {
        try {
            if (conn != null) {
                conn.setAutoCommit(false);
            }
        } catch (Exception e) {
            logger.error(MethodUtil.printExceptionErr(e));
        }
    }

    /**
     * 提交事务操作
     */
    protected void commit(Connection conn) {
        if (conn != null) {
            try {
                conn.commit();
                conn.setAutoCommit(true);
            } catch (Exception e) {
                logger.error(MethodUtil.printExceptionErr(e));
            }
        }
    }

    /**
     * 事务回滚操作
     */
    protected void rollback(Connection conn) {
        if (conn != null) {
            try {
                if (logger.isDebugEnabled()) {
                    logger.debug("++++ transaction rollback ++++");
                }
                conn.rollback();
            } catch (Exception e) {
                logger.error(MethodUtil.printExceptionErr(e));
            }
        }
    }

    /**
     * 关闭数据库连接
     */
    protected void closeConn(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                logger.error(MethodUtil.printExceptionErr(e));
            }
        }
    }

    /**
     * 关闭Sql声明
     */
    protected void closeStatement(Statement stmt) {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                logger.error(MethodUtil.printExceptionErr(e));
            }
        }
    }

    /**
     * 关闭结果集
     */
    protected void closeResultSet(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                logger.error(MethodUtil.printExceptionErr(e));
            }
        }
    }

    /**
     * 关闭数据库资源的方法
     */
    protected void closeResource(Connection conn, Statement stmt, ResultSet rs) {
        // 关闭结果集
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                logger.error(MethodUtil.printExceptionErr(e));
            }
        }
        // 关闭Sql声明
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                logger.error(MethodUtil.printExceptionErr(e));
            }
        }
        // 关闭数据库连接
        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                logger.error(MethodUtil.printExceptionErr(e));
            }
        }
    }

}
